package com.usian.wemedia.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.wemedia.WmNewsControllerApi;
import com.usian.wemedia.service.WmNewsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 文章列表分页查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @Override
    public ResponseResult findNewsList(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findNewsList(dto);
    }


    /**
     * 保存文章
     *
     * @param dto
     * @return
     */
    @PostMapping("/submit")
    @Override
    public ResponseResult saveNews(@RequestBody WmNewsDto dto) {

        return wmNewsService.saveNews(dto, dto.getStatus());
    }


    /**
     * 根据id查看文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/one/{id}")
    @Override
    public ResponseResult findNewsById(@PathVariable Integer id) {
        return wmNewsService.findNewsById(id);
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @GetMapping("/del_news/{id}")
    @Override
    public ResponseResult delNews(@PathVariable Integer id) {
        return wmNewsService.delNews(id);
    }

    /**
     * 文章上下架 1上架0下架
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }


    /**
     * 根据id查询文章详情，给审核提供远程调用
     *
     * @param id
     * @return
     */
    @GetMapping("/findOne/{id}")
    @Override
    public WmNews findByNewsId(@PathVariable Integer id) {
        return wmNewsService.getById(id);
    }

    /**
     * 更改文章审核状态 给审核提供远程调用接口
     *
     * @param wmNews
     * @return
     */
    @PostMapping("/update")
    @Override
    public ResponseResult updateNewsStatus(@RequestBody WmNews wmNews) {

        boolean update = wmNewsService.update(Wrappers.<WmNews>lambdaUpdate().set(WmNews::getStatus, wmNews.getStatus()).set(WmNews::getReason, wmNews.getReason()).eq(WmNews::getId, wmNews.getId()));
        if(update){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }


    /**
     * 审核通过待发布文章列表
     *
     * @return
     */
    @GetMapping("/findNewsListShenhe")
    @Override
    public List<Integer> findNewsListShenhe() {
        return wmNewsService.findNewsListShenhe();
    }


    /**
     * 人工审核列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/authList")
    @Override
    public PageResponseResult findNewsRenGongPage(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findNewsRenGongPage(dto);
    }

    /**
     * 根据id查询文章详情 提供admin远程调用
     *
     * @param id
     * @return
     */
    @GetMapping("/findNewsOne/{id}")
    @Override
    public ResponseResult findOne(@PathVariable Integer id) {
        return wmNewsService.findOne(id);
    }

    /**
     * 更新人工审核状态 提供给admin远程调用
     * @param dto
     * @return
     */
    @RequestMapping("/authNewsRenGong")
    @Override
    public ResponseResult authNewsRenGong(@RequestBody NewsAuthDto dto) {
        return wmNewsService.authNewsRenGong(dto);
    }
}
